set
	I    customers /i1 * i50/   
	W    scenarios /w1 * w50/  
;

OPTIONS ITERLIM  = 100000, RESLIM   = 10000, LIMROW   = 10,
        LIMCOL   = 0,     SYSOUT   = OFF,   SOLPRINT = OFF,
        LP = cplex, NLP=MINOS5, MIP=convert,  OPTCR = 0.0001,
	SEED = 1500;

$onecho > cplex.opt 
heurfreq -1
cuts -1
preind 0
$offecho 

Parameters
	d(i, w)  demand at j in scenario w
	c(i)  unit cost to install capacity
	p(w)     probability of scenario w
	bigm(w)
	bigm_cheat(i, w)
;

d(i, w) = normal(1, 0.2);
c(i) = uniform(0, 1);
p(w) = 1 / card(w);
bigm(w) = smax(i, d(i, w));
*bigm_cheat(i, w) = d(i,w)
*bigm(w) = 100;
*bigm(w) = smax(j, d(j,w));

Scalars
	epsilon;
epsilon = 0.2;

Variables
	x(i)
	y(w)
	z
;
Binary variable y;
Positive variable x;

Equations
	cost
	risk
	chance_demand(i, w)  for each custormer and scenario
	chance_demand_cheat(i,w)
;

cost..                    z   =e= sum( i, x(i) * c(i) );
risk..                epsilon =l= sum( w, p(w) * y(w) );
chance_demand(i, w)..       0 =g= d(i, w) - x(i) + bigm(w) * y(w);
chance_demand_cheat(i,w)..  0 =g= d(i, w) - x(i) + bigm_cheat(i, w) * y(w);

* Create the model
Model stoch_fac            /cost, risk, chance_demand/;
stoch_fac.optfile=1
Model stoch_fac_cheat /cost, risk, chance_demand_cheat/;

* Solve
Solve stoch_fac using mip minimizing z;
display x.L, y.L;

* Compute the cheated big-M and solve 
bigm_cheat(i, w) = abs(x.L(i) - d(i, w));
Solve stoch_fac_cheat using mip minimizing z;
display x.L, y.L;
display bigm, bigm_cheat;




	
	
	